package cn.fuzongyao.learning.android.crime16;

import android.app.Dialog;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.FileProvider;
import androidx.fragment.app.DialogFragment;

import java.io.File;

public class PhotoFragment extends DialogFragment {
    private static final String ARG_PHOTO = "photo";

    public static PhotoFragment newInstance(String id) {
        Bundle args = new Bundle();
        args.putSerializable(ARG_PHOTO, id);
        PhotoFragment photoFragment = new PhotoFragment();
        photoFragment.setArguments(args);
        return photoFragment;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        String id = getArguments().getString(ARG_PHOTO);
        Crime crime = CrimeLab.getInstance(getActivity()).getCrime(id);
        File photoFile = CrimeLab.getInstance(getActivity()).getPhotoFile(crime);
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_photo, null);
        ImageView imageView = view.findViewById(R.id.dialog_photo);
        Uri uri = FileProvider.getUriForFile(getActivity(), CrimeFragment.PHOTO_PATH, photoFile);
        imageView.setImageURI(uri);
        return new AlertDialog.Builder(getActivity())
                .setView(view)
                .setPositiveButton(android.R.string.ok, null)
                .create();
    }
}
